//
// ResizeDialog.java
//
package com.photoexpert;

import java.awt.image.BufferedImage;

class ResizeDialog extends javax.swing.JDialog {

    static ResizeDialog Instance() {
        if(_instance == null)
            _instance = new ResizeDialog();
        return _instance;
    }

    void display() {
        BufferedImage image = Implementor.Instance().getImage();
        if(image != null) {
            _originalWidth = image.getWidth();
            _originalHeight = image.getHeight();
            _width.setText(Integer.toString(_originalWidth));
            _height.setText(Integer.toString(_originalHeight));
            _width.setEnabled(true);
            _height.setEnabled(true);
        } else {
            _originalWidth = 0;
            _originalHeight = 0;
            _width.setText("0");
            _height.setText("0");
            _width.setEnabled(false);
            _height.setEnabled(false);
        }
        setVisible(true);
    }

    private ResizeDialog() {
        super(App.Instance(), "Resize", true);
        initComponents();
        setLocation(200, 200);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        _width = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        _height = new javax.swing.JTextField();
        _keepRatio = new javax.swing.JCheckBox();
        _apply = new javax.swing.JButton();
        _cancel = new javax.swing.JButton();

        setTitle("Resize");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setIconImage(null);
        setIconImages(null);
        setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
        setResizable(false);

        jLabel1.setText("Width");
        jLabel1.setName("widthLabel"); // NOI18N

        _width.setText("0");
        _width.setName("_width"); // NOI18N
        _width.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                _widthFocusLost(evt);
            }
        });

        jLabel2.setText("Height");
        jLabel2.setName("heightLabel"); // NOI18N

        _height.setText("0");
        _height.setName("_height"); // NOI18N
        _height.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                _heightFocusLost(evt);
            }
        });

        _keepRatio.setSelected(true);
        _keepRatio.setText("Keep Width and Height ratio");
        _keepRatio.setName("_keepRatio"); // NOI18N

        _apply.setText("Apply");
        _apply.setName("_apply"); // NOI18N
        _apply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _applyActionPerformed(evt);
            }
        });

        _cancel.setText("Cancel");
        _cancel.setName("_cancel"); // NOI18N
        _cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _cancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2))
                                .addGap(7, 7, 7)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(_height)
                                    .addComponent(_width, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)))
                            .addComponent(_keepRatio)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addComponent(_apply, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(_cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(33, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(_width, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(_height, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addComponent(_keepRatio)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(_cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(_apply, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void _widthFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event__widthFocusLost
        if(_keepRatio.isSelected()) {
            int width = Integer.parseInt(_width.getText());
            int height = (int)(width * 1.0 / _originalWidth * _originalHeight);
            _height.setText(Integer.toString(height));
        }
    }//GEN-LAST:event__widthFocusLost

    private void _cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__cancelActionPerformed
        setVisible(false);
    }//GEN-LAST:event__cancelActionPerformed

    private void _applyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__applyActionPerformed
        int width = Integer.parseInt(_width.getText());
        int height = Integer.parseInt(_height.getText());
        if(width <= 0 || height <= 0 || width > _originalWidth || height > _originalHeight)
            return;
        Implementor.Instance().resize(width, height, true);
        setVisible(false);
    }//GEN-LAST:event__applyActionPerformed

    private void _heightFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event__heightFocusLost
        if(_keepRatio.isSelected()) {
            int height = Integer.parseInt(_height.getText());
            int width = (int)(height * 1.0 / _originalHeight * _originalWidth);
            _width.setText(Integer.toString(width));
        }
    }//GEN-LAST:event__heightFocusLost

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton _apply;
    private javax.swing.JButton _cancel;
    private javax.swing.JTextField _height;
    private javax.swing.JCheckBox _keepRatio;
    private javax.swing.JTextField _width;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables
    private static ResizeDialog _instance = null;
    private int _originalWidth;
    private int _originalHeight;

}
